package cn.tedu.utils;

import com.alibaba.druid.pool.DruidDataSource;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class DBUtils {
    private static DruidDataSource dataSource;
    static {
        //创建属性配置对象
        Properties p = new Properties();
        //通过反射技术得到info.properties配置文件的输入流
        InputStream ips = DBUtils.class.getClassLoader()
                .getResourceAsStream("jdbc.properties");

        //把文件加载到属性对象中
        try {
            p.load(ips);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //从属性对象中获取数据 属性对象中只能获取到字符串类型
        String driver=p.getProperty("db.driver");
        String url=p.getProperty("db.url");
        String username = p.getProperty("db.username");
        String password=p.getProperty("db.password");
        System.out.println(username+":"+password);


        //创建连接池对象
        dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        //设置连接池出事数量和最大数量
        String initSize = p.getProperty("db.initialSize");
        String maxSize= p.getProperty("db.maxActive");


        dataSource.setInitialSize(Integer.parseInt(initSize));//初始链接数量
        dataSource.setMaxActive(Integer.parseInt(maxSize));//最大链接数量
    }
    public static Connection getConn() throws Exception {


        Connection conn=  dataSource.getConnection();
        System.out.println(conn);

        return conn;
    }
}
